package com.atjava.webspider.gui.table;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class DefaultTableModel extends AbstractTableModel {
	
	private String [] colName;
	private int [] editColumns;
	private List<String []> data;
	private boolean editable;

	public DefaultTableModel(String [] colName) {
		this.colName = colName;
		this.data = new ArrayList<String[]>();
	}
	
	public DefaultTableModel(String [] colName,List<String []> data) {
		this.colName = colName;
		this.data = data;
	}
	
	public DefaultTableModel(String [] colName,int row) {
		this.colName = colName;
		this.data = new ArrayList<String[]>();
		for (int i = 0; i < row; i++) {
			String [] rowData = new String [colName.length];
			data.add(rowData);
		}
	}
	
	public DefaultTableModel(String [] colName,int row,String cellValue) {
		this.colName = colName;
		this.data = new ArrayList<String[]>();
		for (int i = 0; i < row; i++) {
			String [] rowData = new String [colName.length];
			for(int j = 0; j<rowData.length; j++){
				rowData [j] =  cellValue;
			}
			data.add(rowData);
		}
	}

	public void updateData(int rowIndex,String [] rowData) {
		data.set(rowIndex, rowData);
		fireTableDataChanged();
	}
	
	public void updateData(int rowIndex,int columnIndex,String obj) {
		this.data.get(rowIndex)[columnIndex] = obj;
		fireTableDataChanged();
	}
	
	public void updateAllData(List<String []> newData){
		data = newData;
		fireTableDataChanged();
	}
	
	public void addData(String [] row) {
		data.add(row);
		fireTableDataChanged();
	}
	
	public void addAllData(List<String []> data) {
		data.addAll(data);
		fireTableDataChanged();
	}

	public void removeData(int row) {
		data.remove(row);
		fireTableDataChanged();
	}
	
	public void removeHeader(){
		this.colName = new String [0];
		fireTableStructureChanged();
	}
	
	public void removeAllData() {
		data.clear();
		fireTableDataChanged();
	}
	
	public void setHeader(String [] colName){
		this.colName = colName;
		fireTableStructureChanged();
	}
	
	public void setEditable(boolean editable){
		this.editable = editable;
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if(this.editColumns == null || this.editColumns.length == 0)
			return this.editable;
		else{
			for (int i = 0; i < this.editColumns.length; i++) {
				if(this.editColumns[i] == columnIndex)
					return true;
			}
			return false;
		}
	}

	public int getColumnCount() {
		return colName.length;
	}

	public String getColumnName(int col) {
		return colName[col];
	}

	public int getRowCount() {
		return data.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return data.get(rowIndex)[columnIndex];
	}

	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		String [] row = data.get(rowIndex);
		row[columnIndex] = String.valueOf(value);
		fireTableCellUpdated(rowIndex,columnIndex);
	}

	public List<String[]> getData() {
		return data;
	}

	public int[] getEditColumns() {
		return editColumns;
	}

	public void setEditColumns(int[] editColumns) {
		this.editColumns = editColumns;
	}
	
}
